package com.game.logic.items.title;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.constant.AIDCFG;
import com.coment.cfg.luban.items.TitleCfg;
import com.coment.po.roles.Roles;
import com.coment.po.UserPlayInfo;
import com.game.dao.user.UserPlayInfoDao;
import com.game.dao.user.roles.RolesDao;
import com.game.logic.fighting.attrs.AttrsManagement;
import com.game.logic.items.title.message.UserTitleList;
import com.xs.action.exection.ActionException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/// 6.称号系统
@Slf4j
@Service
public class UserTitleService {
    @Resource
    public UserTitleDao userTitleDao;

    @Resource
    private RolesDao rolesDao;

    @Resource
    private UserPlayInfoDao userPlayInfoDao;


    @Resource
    private AttrsManagement attrsManagement;


    /// 获取称号
    public void obtain(Integer userInfoId, Integer title) {
        UserTitle insert = userTitleDao.insert(new UserTitle(
                userInfoId,
                title
        ));
        attrsManagement.logAttrsGlobal(userInfoId, insert.getAttrs(), "Title", userInfoId, title);
    }

    /// 领取称号
    public void claim(Integer userInfoId, Integer title) {
        TitleCfg titleCfg = Tables.cfg.get_tbtitlecfg().get(title);
        switch (titleCfg.type) {
            case 1:
                int cardId = titleCfg.cardId;
                int needValue = titleCfg.needLv;
                Roles roles = rolesDao.searchByUserInfoIdAndCardId(userInfoId, cardId);
                if (roles.rolePanel.getAttribute().get(AIDCFG.level) >= needValue) {
                   obtain(userInfoId, title);
                }
                break;
            case 2, 3:
                break;
        }
    }

    /// 佩戴称号
    public UserTitle wear(Integer userInfoId, Integer title) {
        UserTitle select = userTitleDao.findMust(userInfoId, title);
        if (select == null) throw new ActionException("没有这个称号");

        UserPlayInfo byUserInfoIdOrCreate = userPlayInfoDao.findByUserInfoIdOrCreate(userInfoId);

        Object[] tag = new Object[]{"WearTitle", userInfoId};
        if (byUserInfoIdOrCreate.getRecentlyPlayedTitleId() != null) {
            attrsManagement.clearAttrsGlobal(userInfoId, tag);
        }
        byUserInfoIdOrCreate.setRecentlyPlayedTitleId(title);
        attrsManagement.logAttrsGlobal(userInfoId, select.getAttrs(), tag);

        userPlayInfoDao.save(byUserInfoIdOrCreate);
        return select;
    }

    public UserTitleList list(Integer userInfoId) {
        for (TitleCfg cfg : Tables.cfg.get_tbtitlecfg().get_dataList()) {
            try {
                this.claim(userInfoId, cfg.id);
            } catch (Exception e) {
                log.error("获取称号失败: {}", cfg.id);
            }
        }
        List<UserTitle> userTitles = userTitleDao.searchAllByUserInfoId(userInfoId);
        UserPlayInfo userPlayInfo = userPlayInfoDao.findByUserInfoIdOrCreate(userInfoId);

        UserTitleList userTitleList = new UserTitleList();
        userTitleList.titles = userTitles.stream().mapToInt(UserTitle::getTitle).boxed().toList();
        userTitleList.wear = userPlayInfo.getRecentlyPlayedTitleId();

        return userTitleList;
    }

    public UserTitle select(Integer userInfoId, Integer title) {
        this.claim(userInfoId, title);
        return userTitleDao.findMust(userInfoId, title);
    }
}
